package com.stedu.controller;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.IdUtil;
import com.stedu.bean.RespBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
public class CaptchaController extends BaseController {
    @GetMapping("/captcha")
    public RespBean captcha() {
        //创建验证码对象
        LineCaptcha captcha = new LineCaptcha(120, 35, 4, 10);

        //获取验证码文本
        String captchaCode = captcha.getCode();

        //通过雪花算法生成唯一ID
        String captchaId = IdUtil.getSnowflakeNextIdStr();

        //将验证码ID和验证码文本存入redis
        redisUtil.set(captchaId, captchaCode, 60 * 5);

        //将验证码装换为base64文本
        String captchaImageBase64Data = captcha.getImageBase64Data();

        //创建map保存验证码ID和Base64文本
        HashMap<String, Object> map = new HashMap<>();
        map.put("captchaId", captchaId);
        map.put("captchaImageBase64Data", captchaImageBase64Data);

        //以json格式返回验证码ID和Base64文本
        return RespBean.ok("获取验证码成功",map);
    }
}
